/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.edu.vttu.dichvu;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import vn.edu.vttu.readxml.ReadXML;
import vn.edu.vttu.readxml.thongtindb;

/**
 *
 * @author nhphuoc
 */
public class QuanLy_DichVu extends javax.swing.JFrame {

    class ExcelExporter {

        public ExcelExporter() {
        }

        public void exportTable(JTable table, File file) throws IOException {
            TableModel model = table.getModel();
            FileWriter out = new FileWriter(file);
            for (int i = 0; i < model.getColumnCount(); i++) {
                out.write(model.getColumnName(i) + "\t");
            }
            out.write("\n");

            for (int i = 0; i < model.getRowCount(); i++) {
                for (int j = 0; j < model.getColumnCount(); j++) {
                    //   I added this check for the ISBN conversion
                    if (j == 0) {
                        //    the book Title
                        out.write(model.getValueAt(i, j).toString() + "\t");
                    } else {
                        /*
                         the ISBN Number
                         Note that I added a \" to the front of the string
                         and a \t followed by a closing \" to let Excel know
                         that this field is to be converted as text
                         */
                        out.write("\"" + model.getValueAt(i, j).toString() + "\t" + "\"");
                    }
                }
                out.write("\n");
            }
            out.close();
            System.out.println("write to " + file);
        }
    }

    /**
     * Creates new form QuanLy_DichVu
     */
    private boolean them = false;

    public QuanLy_DichVu() {
        initComponents();
        loaddv();
        setLocation(230,120);
        enableButton(true);
        tendv.setEnabled(false);
        dongia.setEnabled(false);
    }

    private void loaddv() {
        try {
            ReadXML read = new ReadXML();
            Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
            vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
            TableModel tb = common.LoadDV();
            tbDichVu.setModel(tb);
            this.tbDichVu.setRowSelectionInterval(0, 0);
            madv.setText(String.valueOf(tbDichVu.getValueAt(0, 0)));
            tendv.setText(String.valueOf(tbDichVu.getValueAt(0, 1)));
            dongia.setText(String.valueOf(tbDichVu.getValueAt(0, 2)));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void enableButton(boolean b) {
        buttonthem.setEnabled(b);
        buttonSua.setEnabled(b);
        buttonXoa.setEnabled(b);
        buttonLuu.setEnabled(!b);

        tbDichVu.setEnabled(b);

    }

    private boolean testNumber(String num) {
        boolean test = false;
        if (num.equals("")) {
            test = false;
        } else {
            for (int i = 0; i < num.length(); i++) {
                if (Character.isLetter(num.charAt(i))) {
                    test = false;
                    break;
                }
                if (i + 1 == num.length()) {
                    test = true;
                }
            }
        }
        return test;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        madv = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tendv = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        dongia = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        buttonthem = new javax.swing.JButton();
        buttonSua = new javax.swing.JButton();
        buttonXoa = new javax.swing.JButton();
        buttonLuu = new javax.swing.JButton();
        butttonReload = new javax.swing.JButton();
        buttonPrint = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDichVu = new javax.swing.JTable();
        timkiem = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 255));
        jLabel1.setText("QUẢN LÝ DỊCH VỤ");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin chi tiết", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jLabel2.setText("Mã DV:");

        madv.setEditable(false);

        jLabel3.setText("Tên DV:");

        jLabel4.setText("Đơn Giá");

        dongia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                dongiaKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(madv, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                            .addComponent(tendv)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dongia)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(madv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tendv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(dongia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Chức năng"));

        buttonthem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/Bookmark-add-icon.png"))); // NOI18N
        buttonthem.setText("Thêm");
        buttonthem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonthemActionPerformed(evt);
            }
        });

        buttonSua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/edit-validated-icon.png"))); // NOI18N
        buttonSua.setText("Sửa");
        buttonSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSuaActionPerformed(evt);
            }
        });

        buttonXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/Actions-edit-delete-icon.png"))); // NOI18N
        buttonXoa.setText("Xóa");
        buttonXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonXoaActionPerformed(evt);
            }
        });

        buttonLuu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/Floppy-Small-icon.png"))); // NOI18N
        buttonLuu.setText("Lưu");
        buttonLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLuuActionPerformed(evt);
            }
        });

        butttonReload.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/button-round-reload-icon.png"))); // NOI18N
        butttonReload.setText("Reload");
        butttonReload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butttonReloadActionPerformed(evt);
            }
        });

        buttonPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/printer-icon.png"))); // NOI18N
        buttonPrint.setText("Print");
        buttonPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonPrintActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(buttonthem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(butttonReload, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonSua, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonXoa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonLuu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonPrint, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(buttonthem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonSua)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonXoa)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonLuu)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(butttonReload)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonPrint)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách dịch vụ"));

        tbDichVu.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbDichVu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDichVuMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDichVu);

        timkiem.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                timkiemKeyTyped(evt);
            }
        });

        jLabel5.setText("Tìm DV:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(timkiem, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(0, 0, 0))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(timkiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(5, 5, 5)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(308, 308, 308)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(353, 353, 353))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonthemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonthemActionPerformed
        them = true;
        tendv.setText("");
        dongia.setText("");
        madv.setText("");
        tendv.setEnabled(true);
        dongia.setEnabled(true);
        enableButton(false);
    }//GEN-LAST:event_buttonthemActionPerformed

    private void buttonSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSuaActionPerformed
        them = false;
        tendv.setEnabled(true);
        dongia.setEnabled(true);
        enableButton(false);
    }//GEN-LAST:event_buttonSuaActionPerformed

    private void buttonLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLuuActionPerformed
        String tendichvu = "";
        int dongiadv;
        if (tendv.getText().equals("") || dongia.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập tên dịch vụ hoặc đơn giá");
            if (tendv.getText().equals("")) {
                tendv.requestFocus();
            } else {
                dongia.requestFocus();
            }
        } else if (Integer.parseInt(dongia.getText().toString()) < 0) {
            JOptionPane.showMessageDialog(rootPane, "Bạn nhập đơn giá sai");
            dongia.requestFocus();
        } else if (testNumber(dongia.getText().toString()) == false) {
            JOptionPane.showMessageDialog(rootPane, "Bạn nhập đơn giá sai");
            dongia.requestFocus();
        } else {
            tendichvu = tendv.getText().trim();

            if (them == true) {
                try {
                    dongiadv = Integer.parseInt(dongia.getText().toString());
                    ReadXML read = new ReadXML();
                    Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
                    vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
                    boolean x = common.insertDichVu(tendichvu, dongiadv);
                    if (x) {
                        JOptionPane.showMessageDialog(rootPane, "Thêm dịch vụ thành công");
                        loaddv();
                        enableButton(true);
                    } else {
                        JOptionPane.showMessageDialog(rootPane, "Thêm dịch vụ không thành công");
                        loaddv();
                        enableButton(true);
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(rootPane, "Đã xay ra lỗi: " + ex.getMessage());
                    loaddv();
                    enableButton(true);
                    enableButton(true);
                }

            } else {
                try {
                    int id = Integer.parseInt(madv.getText().toString());
                    dongiadv = Integer.parseInt(dongia.getText().toString());
                    ReadXML read = new ReadXML();
                    Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
                    vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
                    boolean x = common.updateDichVu(id, tendichvu, dongiadv);
                    if (x) {
                        JOptionPane.showMessageDialog(rootPane, "Cập nhật dịch vụ thành công");
                        loaddv();
                        enableButton(true);
                    } else {
                        JOptionPane.showMessageDialog(rootPane, "Cập nhật dịch vụ không thành công");
                        loaddv();
                        enableButton(true);
                    }
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(rootPane, "Bạn nhập sai đơn giá");
                    dongia.requestFocus();
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(rootPane, "Đã xay ra lỗi: " + ex.getMessage());
                    loaddv();
                    enableButton(true);
                    enableButton(true);
                }
            }
        }
    }//GEN-LAST:event_buttonLuuActionPerformed

    private void buttonXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonXoaActionPerformed
        if (dongia.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Chưa chọn dịch vụ để xóa");
        } else {
            int i = JOptionPane.showConfirmDialog(this, "Bạn có muốn xóa dữ liệu này không?", "Cảnh Báo", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (i == 0) {
                try {
                    int id = Integer.parseInt(madv.getText().toString());
                    ReadXML read = new ReadXML();
                    Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
                    vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
                    boolean x = common.DeleteDichVu(id);
                    if (x) {
                        JOptionPane.showMessageDialog(rootPane, "Xóa dịch vụ thành công");
                        loaddv();
                        enableButton(true);
                    } else {
                        JOptionPane.showMessageDialog(rootPane, "Xóa dịch vụ không thành công");
                        loaddv();
                        enableButton(true);
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(rootPane, "Đã xay ra lỗi: " + ex.getMessage());
                    loaddv();
                    enableButton(true);
                    enableButton(true);
                }
            }
        }
    }//GEN-LAST:event_buttonXoaActionPerformed

    private void dongiaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dongiaKeyTyped
        int key = evt.getKeyChar();
        String st = dongia.getText();
        String stTest = "0123456789";
        if (key != evt.VK_BACK_SPACE
                && key != evt.VK_DELETE
                && key != evt.VK_ENTER) {
            int flag = 0;
            if (stTest.indexOf(evt.getKeyChar()) == -1) {
                flag++;
                JOptionPane.showMessageDialog(this, "Chỉ được nhập số");
            }
            if (flag > 0) {
                evt.consume();
            }
        }
    }//GEN-LAST:event_dongiaKeyTyped

    private void tbDichVuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDichVuMouseClicked
        int index = tbDichVu.getSelectedRow();
        madv.setText(String.valueOf(tbDichVu.getValueAt(index, 0)));
        tendv.setText(String.valueOf(tbDichVu.getValueAt(index, 1)));
        dongia.setText(String.valueOf(tbDichVu.getValueAt(index, 2)));
    }//GEN-LAST:event_tbDichVuMouseClicked

    private void butttonReloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butttonReloadActionPerformed
        loaddv();
    }//GEN-LAST:event_butttonReloadActionPerformed

    private void timkiemKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_timkiemKeyTyped
        try {
            ReadXML read = new ReadXML();
            Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
            vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
            TableModel tb = common.TimDichVu(timkiem.getText().toString().trim());
            tbDichVu.setModel(tb);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_timkiemKeyTyped

    private void buttonPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPrintActionPerformed
        String link = "src\\vn\\edu\\vttu\\dichvu\\rp_danhsachDichVu.jrxml";
        try {
            //Tạo kết nối đến Mysql
            
            Class.forName("com.mysql.jdbc.Driver");
            String database = "jdbc:mysql://"+thongtindb.getIp()+":"+thongtindb.getPort()+"/"+thongtindb.getDb();
            String us = thongtindb.getU();
            String pa = thongtindb.getP();
            Map<String, Object> params = new HashMap<String, Object>();  
            Connection conn = (Connection) DriverManager.getConnection(database, us, pa);
            // tạo JasperReport
            JasperReport jr = JasperCompileManager.compileReport(link);
            //tạo JasperPrint
            JasperPrint jp = JasperFillManager.fillReport(jr, params, conn);            
            JasperViewer jv = new JasperViewer(jp, false); 
            jv.setVisible(true); 
        } catch (Exception ex) {

        }
    }//GEN-LAST:event_buttonPrintActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(QuanLy_DichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(QuanLy_DichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(QuanLy_DichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(QuanLy_DichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new QuanLy_DichVu().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonLuu;
    private javax.swing.JButton buttonPrint;
    private javax.swing.JButton buttonSua;
    private javax.swing.JButton buttonXoa;
    private javax.swing.JButton buttonthem;
    private javax.swing.JButton butttonReload;
    private javax.swing.JTextField dongia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField madv;
    private javax.swing.JTable tbDichVu;
    private javax.swing.JTextField tendv;
    private javax.swing.JTextField timkiem;
    // End of variables declaration//GEN-END:variables
}
